
import { onMounted,ref } from "vue";
import { getTopCategoryAPI } from "@/apis/category";
import { useRoute,onBeforeRouteUpdate  } from "vue-router";


export function useCategory() {
  // 获取路由数据
  const categoryData = ref([]);
  const route = useRoute();
  const getCategory = async (id) => {
    if (!id) return; // 没有 categoryId 时跳过
    const res = await getTopCategoryAPI(id);
    categoryData.value = res.data.result;
  };

  // 在组件挂载时获取分类数据
  onMounted(() => getCategory(route.params.id));
  // 在路由参数更新时重新获取分类数据
  onBeforeRouteUpdate((to, from, next) => {
    getCategory(to.params.id);
    next(); // 确保路由更新继续进行
  });
  return {
    categoryData
  }
}
